home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / bldlib.c < prev    next >
C/C++ Source or Header  |  1989-10-18  |  6KB  |  144 lines

  1. /**************************************************************************/
  2. /* Copyright (c) 1989 by Daniel R. Zemke -- CompuServe [73230,1543]       */
  3. /*                                                                        */
  4. /* Purpose:     Creates one or more libraries from all "*.c" files        */
  5. /*              in the current directory.                                 */
  6. /*                                                                        */
  7. /* Example:     Executing      buildlib mylib scl                         */
  8. /*                                                                        */
  9. /*              Creates: mylibS.lib, mylibC.lib AND mylibL.lib            */
  10. /*              (i.e. small, compact and large library versions of MYLIB) */
  11. /*                                                                        */
  12. /* Parameters:  Two and only two parameters are required.  The first is   */
  13. /*              used to specify the common name prefix for the series     */
  14. /*              of generated libraries.  The second parameter is a char   */
  15. /*              string of one or more characters indicating desired       */
  16. /*              memory models.  Each library name is formed by suffixing  */
  17. /*              the library series name with a character from the memory  */
  18. /*              model list.  The memory model list consists of            */
  19. /*              contiguous characters without any surrounding quotes.     */
  20. /*              Valid characters for the memory model list are :          */
  21. /*                         t s m c l h                                    */
  22. /*                                                                        */
  23. /* Assumptions: Turbo C for compile of source and DOS 2+ for execution.   */
  24. /*              TCC and TLIB are in the current search path.              */
  25. /*              Every "*.c" file in the current directory is to be        */
  26. /*                 included in target lib(s).                             */
  27. /*              Adequate memory to invoke tcc from within BUILDLIB        */
  28. /*                 using the system() call.                               */
  29. /**************************************************************************/
  30.  
  31. #include <dir.h>
  32. #include <process.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #define MAX_LIBNAME_CHARS 7
  37. #define MAX_CMD_SIZE      255
  38.  
  39. void add_obj(char *lib_add_cmd, char *obj_name)
  40. /*********************************************************/
  41. /* Adds "obj_name" to lib and then deletes the original. */
  42. /*********************************************************/
  43. {
  44. char cmd[MAX_CMD_SIZE];
  45.  
  46.    sprintf(cmd,"%s%s",lib_add_cmd,obj_name);
  47.    system(cmd);
  48.    sprintf(cmd,"del %s",obj_name);
  49.    system(cmd);
  50. }
  51.  
  52. void create_lib(char *lib_name)
  53. /**********************************************************************/
  54. /* Creates a library out of all object files in the current directory */
  55. /**********************************************************************/
  56. {
  57. char cmd[MAX_CMD_SIZE];
  58. struct ffblk dos_fcb;
  59.  
  60.    if ( findfirst("*.obj",&dos_fcb,0) == 0) {
  61.        sprintf(cmd,"del %s.lib",lib_name);   /*Attempt delete "lib_name.lib"*/
  62.        system(cmd);
  63. /* Create lib from "*.obj" in current directory */
  64.        sprintf(cmd,"tlib %s /E +",lib_name);
  65.        add_obj(cmd,dos_fcb.ff_name);
  66.        while(findnext(&dos_fcb) == 0)
  67.           add_obj(cmd,dos_fcb.ff_name);
  68.    }
  69.  
  70. /* Delete "lib_name.bak" created by using "/E" for extended directory */
  71.    sprintf(cmd,"del %s.bak",lib_name);
  72.    system(cmd);
  73. }
  74.  
  75. void compile(char *lib_type)
  76. /**********************************************************************/
  77. /* Compiles all ".c" files in the current directory with memory model */
  78. /* specified by "lib_type".                                           */
  79. /**********************************************************************/
  80. {
  81. static char *lib_type_pos = NULL;
  82. #define LIB_TYPE_POS      '^'     /* change compile_cmd if this is changed */
  83. static char *compile_cmd  = "tcc -m^ -k- -a -c -G -O -Z -d *.c";
  84. /***********************************************************************/
  85. /* The above variable specifies the compile options for building libs. */
  86. /* Note : The '^' after the "-m" is required and will be replaced by a */
  87. /*        character indicating a valid memory model during execution.  */
  88. /***********************************************************************/
  89.  
  90.    if (lib_type_pos == NULL)
  91.       lib_type_pos = strchr(compile_cmd,LIB_TYPE_POS);
  92.  
  93.    *lib_type_pos = *lib_type;
  94.  
  95.    system(compile_cmd);
  96. }
  97.  
  98. int chk_parms(int argc, char **argv)
  99. /************************************************/
  100. /* Checks that program parameters are resonable */
  101. /************************************************/
  102. {
  103.    if (argc != 3) {
  104.       fprintf(stderr,"2 and only 2 parameters are required\n");
  105.       fprintf(stderr,"   1st parm specifies base lib_name\n");
  106.       fprintf(stderr,"   2nd parm is a char list of memory models : tsmclh\n");
  107.       return(1);
  108.    }
  109.  
  110.    if (strlen(argv[1]) > MAX_LIBNAME_CHARS) {
  111.       fprintf(stderr,"Length of base lib_name is too long\n");
  112.       return(1);
  113.    }
  114.  
  115.    if ( strspn(argv[2],"tsmclh") != strlen(argv[2]) ) {
  116.       fprintf(stderr,"Invalid memory model specified in 2nd parm\n");
  117.       return(1);
  118.    }
  119.  
  120.    return(0);
  121. }
  122.  
  123. int main(int argc, char **argv)
  124. {
  125. char lib_name[MAX_LIBNAME_CHARS + 2];
  126. char *lib_mod;
  127. char *lib_type;
  128.  
  129.    if (chk_parms(argc,argv))
  130.       return(1);
  131.  
  132.    strcpy(lib_name,argv[1]);
  133.    lib_mod = lib_name + strlen(lib_name);
  134.    strcat(lib_name," ");
  135.  
  136.    for (lib_type = argv[2]; *lib_type; lib_type++) {
  137.       compile(lib_type);
  138.       *lib_mod = *lib_type;
  139.       create_lib(lib_name);
  140.    }
  141.  
  142.    return(0);
  143. }
  144.